package org.wisigoth.chat.swing;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.UIManager;

import org.wisigoth.chat.client.ChatSession;
import org.wisigoth.chat.client.Contact;
import org.wisigoth.chat.client.Message;
import org.wisigoth.chat.client.MessageConsumer;
import org.wisigoth.chat.client.MessageFilter;
import org.wisigoth.chat.client.Session;
import org.wisigoth.chat.client.Session.ConnectionState;
import org.wisigoth.chat.swing.accountframe.ContactListController;
import org.wisigoth.chat.swing.accountframe.ContactListFrame;
import org.wisigoth.chat.swing.accountframe.LoginController;
import org.wisigoth.chat.swing.chatscreen.ChatController;
import org.wisigoth.chat.swing.chatscreen.ChatPanel;

import com.nilo.plaf.nimrod.NimRODLookAndFeel;
import com.nilo.plaf.nimrod.NimRODTheme;

public class WisigothController implements ActionListener, MessageConsumer,
		MessageFilter, MouseListener {

	private final Session session;

	private final LoginController loginController;
	private final ContactListController contactListController;
	private final WisigothSystemTray iconTray;
	private final ChatController chatController;

	public WisigothController(Session session) {
		this.session = session;
		try {
			NimRODTheme nt = new NimRODTheme();
			nt.setPrimary1(new Color(0x85B8EB));
			nt.setPrimary2(new Color(0x8FC2F5));
			nt.setPrimary3(new Color(0x99CCFF));
			
			nt.setSecondary1(new Color(0xE2E6E8));
			nt.setSecondary2(new Color(0xECF0F2));
			nt.setSecondary3(new Color(0xF6FAFC));
			
			nt.setBlack(new Color(0x000000));
			nt.setWhite(new Color(0xFFFFFF));

			nt.setFrameOpacity(180);
			nt.setMenuOpacity(195);

			
			NimRODLookAndFeel NimRODLF = new NimRODLookAndFeel();
			NimRODLF.setCurrentTheme(nt);
			UIManager.setLookAndFeel(NimRODLF);
			
			// UIManager.setLookAndFeel(new NapkinLookAndFeel());
		} catch (Exception e) {
			System.out.println("Error setting LAF: " + e);
		}
		this.session.setUnconsumedMessageConsumer(this);
		this.loginController = new LoginController(this);
		this.contactListController = new ContactListController(this);
		this.chatController = new ChatController(this);

		this.iconTray = WisigothSystemTray.getInstance(session);
		this.iconTray.getPopupMenu().addActionListener(this);
		this.iconTray.setMenuOffline();
		this.iconTray.getTrayIcon().addMouseListener(this);

	}

	public Session getSession() {
		return this.session;
	}

	public void setContactListVisible(boolean visible) {
		this.contactListController.setContactListVisible(visible);
	}

	public void showLoginWindow() {
		this.loginController.showLoginWindow();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "Contact List") {
			this.setContactListVisible(true);
		} else if (e.getActionCommand() == "Quit") {
			this.session.quitWisigoth();
		}

	}

	public void openChatSession(Contact contact) {
		if (!this.chatController.isChatted(contact)) {
			ChatSession chatSession = this.getSession().openChat(contact);
			this.chatController.openChat(chatSession);
		} else {
			this.chatController.setFocus(contact);
		}

	}

	@Override
	public void consume(Message message) {
		Contact sender = message.getFrom().toContact();
		// this.openChatSession(sender);
		ChatSession chatSession = this.getSession().openChat(sender);
		this.chatController.openChat(chatSession);
		ChatPanel chatPanel = this.chatController.getChatPanel(chatSession);
		this.chatController.refreshChat(chatPanel, message);
	}

	@Override
	public MessageFilter getFilter() {
		return this;
	}

	@Override
	public boolean accept(Message message) {
		return true;
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getClickCount() == 2) {
			if (WisigothController.this.session.getConnectionState() == ConnectionState.ONLINE) {
				ContactListFrame conList = WisigothController.this.contactListController
						.getContactList();
				conList.setVisible(!conList.isVisible());
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent e) {
	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

}
